import http from "@/api";

const url = "/comm/comhximapi/reguser";
const msgStoreUrl = "/comm/comhximapi/savehximmsg";
const getMsgUrl = "/comm/comhximapi/gethximmsg";

export const getIMUserToken = (token: string) => {
  return http.post(url, { token });
};

export type InnerTxtMsg = {
  ext: any;
  data: Array<{ data: string; type: string }>;
  type?: string;
  url?: string;
};

export type InnerImgMsg = {
  ext: any;
  data: string;
  size: {
    width: string | number;
    height: string | number;
  };
  type?: string;
  url?: string;
};

export type InnerAudioMsg = {
  ext: {
    serverMsgId?: string;
    id?: string;
    body?: {
      filetype?: string;
      filename?: string;
      length?: number | string;
      type?: string;
      url?: string;
    };
  };
  data: string;
  length?: string | number;
  type?: string;
  url?: string;
};

export type InnerCustomMsg = {
  ext: {
    jobId: string;
    type: string;
  };
  data?: string;
  type: string;
  url?: string;
};

export type InnerMsg = InnerTxtMsg | InnerImgMsg | InnerAudioMsg | InnerCustomMsg;

export type StoreMsg = {
  chatType: string;
  type: string | number;
  info:
    | {
        from: string;
        to: string;
      }
    | string;
  msg?: InnerMsg | string;
  time?: string;
};

export const saveMsg = (msg: StoreMsg) => {
  return http.post(msgStoreUrl, msg);
};

export const getMsg = (params: { from: string; to: string; page?: number; pageSize?: number }) => {
  params.pageSize = params.pageSize ? params.pageSize : 50;
  params.page = params.page ? params.page : 1;
  return http.post(getMsgUrl, params);
};
